import crypto from 'crypto-js';
import JSEncrypt from 'uni-encrypt';
// 秘钥
const SECRET = 'MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAMu/EyDFCMnPyhDPDcewXVd7rQRc5c4BjKFdUW+fjssLtDoKqlBma/L9LCVCE0NaRjOr9h3pXkjLYi3MtC+wVSTmI8oy93d3rLhOmsaJ+oDXkKOD+t4/drGTivZjQuSP0gzwlvVRiNLvASVpvi0c1g1M92V5GtALzDxUqdjPTF7vAgMBAAECgYBGJg9L5QrVkPkdOGyiKBK7IBW9vs5QUzA8DokPSXw8ZFc09J8W92yLSKd+XHWNZvNsrvQZ8FNJ9NxksGoJUUi+wj7/w+erB/RfV//5gwR6huWRs3kL8i8pSWUDx2h3Wwz06Q6o/QLYgW47QB+yizSx7fVgbHwkARbcglJoY1oqIQJBAOnTGrpbs1DqZdjY4tdilWinv8QmYQC5c1twh1Zvuc07edP7rcnMjYlOxY+UhQKP1O2MvHHsi5idlUE07ZlczYMCQQDfEbe+Ox1U1B6sxKP+IcXRkbQ4+AerQf3Q1UId+fuRezWxVA1Db/d5InXV9EHS8UCOh/M038A89q2wN/Ci9bklAkBFi0gTzVpT4qNQD5HIRcXyTpI2oMORF5c7xwg5qRk19U6SwfQ6ZOBjyk3wGlhFHspRrN4PdC9AwP23Wyqfxn5JAkBACZgUFmhfN93ufy0NAakGdqukRx/8fB9fWjl23ZLoutQ5KgX8vCHxNoFZgJt88SE1SbX6EdngJnjKQ+TjA2O5AkEA5M7nFtPOPnEmtxO9fj83yBzpkbmFcOpIW98h/FwdkK01ufpylBceVJ4EeDM4z7590WKEANDaxwifWkUsJlF2/g==';
// 签名
const SIGNED = 'jaJHk55KKnYmjKJ';


/**
 * 随机字符
 * @param {number} [8] length 字符长度
 * @returns {string} 指定长度的随机字符串
 */
function randomStr(length = 8) {
  const CHARS = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  let result = '';
  for (let i = 0; i < length; i ++) {
    const index = parseInt(Math.random() * CHARS.length);
    result += CHARS[index];
  }
  return result;
}
// console.log(uni.getStorageSync('Login').token)
/**
 * 
 * @returns {object} 验签参数token
 * */


/**
 * 根据秘钥加密
 * @param {any} value 要加密的值
 * @returns {string} 加密后的值
 * */
function _encrypt(value) {
  const e = new JSEncrypt();
  e.setPublicKey(
    '-----BEGIN PUBLIC KEY-----' + SECRET + '-----END PUBLIC KEY-----'
  );
  return e.encrypt(String(value));
}

/**
 * 根据秘钥加密 若是对象或数组然后对象或数组
 * @param {any} value 要加密的值
 * @returns {string|object|array} 加密后的值
 * */
function encrypt(value) {
	if (typeof value !== 'object') return _encrypt(value);
	
	let result = toString.call(value) === '[object Array]' ? [] : {};
	for (let i in value) {
		result[i] = _encrypt(value[i]);
	}	
	return result;
}

export {  encrypt };
